/**
 * @date 2005-12-15 16:00:32
 *
 */
package org.nanhill.support.dom4j;

import java.net.URL;

import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.DocumentResult;
import org.dom4j.io.DocumentSource;
import org.dom4j.io.SAXReader;

/**
 * @author alin
 * 
 */
public class Dom4jKit {
	public static Document style(Transformer transformer,
			Document document) throws TransformerException {
		// lets style the given document
		DocumentSource source = new DocumentSource(document);
		DocumentResult result = new DocumentResult();
		transformer.transform(source, result);
		// return the transformed document
		Document transformedDoc = result.getDocument();
		return transformedDoc;
	}

	public static Document styleDocument(Transformer transformer, URL xmlUrl)
			throws TransformerException, DocumentException {
		return style(transformer, new SAXReader().read(xmlUrl));
	}

	public static Templates getXsltTemplates(URL xsltUrl)
			throws TransformerConfigurationException {
		TransformerFactory factory = TransformerFactory.newInstance();
		Templates templates = factory.newTemplates(new StreamSource(xsltUrl
				.getFile()));
		return templates;
	}
}
